package academicSup;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import query.Query;
import student.Student;
import user.User;
import user.UserHome;
import degree.DegreeHome;
import department.Department;
import descriptor.AcademicSupDescriptor;
import descriptor.CVDescriptor;

/**
 * AcademicSupBean
 * 
 * 
 * The AcademicBean class implements all the methods that a remote client expect to find
 * when it calls the interface (Academic).
 * 
 * Note :  -We use @SuppressWarnings("unchecked") to avoid the useless warnings
 */
@SuppressWarnings("unchecked")
public abstract class AcademicSupBean implements EntityBean {
	InitialContext initialContext;
	UserHome userHome;
	DegreeHome degreeHome;
	AcademicSupHome academicSupHome;

	// <ATTRIBUTES>
	
	// simple attributes
	public abstract int getId();
	public abstract void setId(int id);
	
	public String getLogin(){
		return getUser().getLogin();
	}
	public void setLogin(String login){
		getUser().setLogin(login);
	}
	
	public String getPassword(){
		return getUser().getPasswd();
	}
	public void setPassword(String password){
		getUser().setPasswd(password);
	}
	
	public String getFirstname(){
		return getUser().getFirstName();
	}
	public void setFirstname(String firstname){
		getUser().setFirstName(firstname);
	}
	
	public String getSurname(){
		return getUser().getName();
	}
	public void setSurname(String surname){
		getUser().setName(surname);
	}
	
	public String getEmail(){
		return getUser().getMail();
	}
	public void setEmail(String email){
		getUser().setMail(email);
	}
	
	// Bean attributes
	// relations 1-1
	public abstract User getUser();
	public abstract void setUser(User user);
	
	public abstract Department getDepartment();
	public abstract void setDepartment(Department dep);
	
	// relation 1-many
	public abstract Collection getQueries();
	public abstract void setQueries(Collection query);
	
	public abstract Collection<Student> getStudents();
	public abstract void setStudents(Collection students);
	// </ATTRIBUTES>
	
	// <BEANS METHODS>
	
	public Collection getDegrees(){
		try {
			initialContext = new InitialContext();
			degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
			return degreeHome.findAllDegrees();
		} catch (FinderException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void acceptQuery(Query query){
		Student s = query.getStudent();
		getStudents().add(s);
		try {
			if(s.getAcademicSup() == null){
				s.setAcademicSup(academicSupHome.findByPrimaryKey(getId()));
			}
		} catch (FinderException e) {
			e.printStackTrace();
		}
		query.setStatus(1);
	}
	public void rejectQuery(Query query){
		Student s = query.getStudent();
		try {
			if(s.getAcademicSup() == null){
				s.setAcademicSup(academicSupHome.findByPrimaryKey(getId()));
			}
		} catch (FinderException e) {
			e.printStackTrace();
		}
		query.setStatus(-1);
	}
	
	public String ejbCreate(String firstName,String name, String login,String passwd,String email,Department dep) throws CreateException{
		try{
			initialContext = new InitialContext();
			userHome = (UserHome)initialContext.lookup("java:comp/env/ejb/User");
			//degreeHome = (DegreeHome)initialContext.lookup("java:comp/env/ejb/Degree");
			degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
			//academicSupHome = (AcademicSupHome)initialContext.lookup("java:comp/env/ejb/AcademicSup");
			academicSupHome = (AcademicSupHome)initialContext.lookup("MyAcademicSup_L");
		}catch (NamingException e) {
			e.printStackTrace();
		}
		User u = null;
		u = userHome.create(firstName, name, login, passwd, email);
		u.setType(User.ACADEMICSUP);
		setId(u.getId());
		
		return null;
	}

	public void ejbPostCreate(String firstName,String name, String login,String passwd,String email,Department dep){
		User u = null;
		try {
			u = userHome.findByPrimaryKey(getId());
		} catch (FinderException e) {
			e.printStackTrace();
		}
		setUser(u);
		setQueries(new Vector());
		setDepartment(dep);
	}
	
	public void ejbActivate() throws EJBException, RemoteException {}
	public void ejbPassivate() throws EJBException, RemoteException {}
	public void ejbRemove() throws EJBException, RemoteException, RemoveException {
		int id = getId();
		User u = null;
		try {
			u = userHome.findByPrimaryKey(id);
		} catch (FinderException e) {
			e.printStackTrace();
		}
		try {
			u.remove();
		} catch (RemoveException e) {
			e.printStackTrace();
		}
	}
	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {}
	public void ejbLoad() throws EJBException, RemoteException {}
	public void ejbStore() throws EJBException, RemoteException {}
	public void setEntityContext(EntityContext arg0) throws EJBException, RemoteException {}
	public void unsetEntityContext() throws EJBException, RemoteException {}
	
	// </ BEANS METHODS>
	
	public AcademicSupDescriptor getDescriptor()
	{
		int [] studentIDs = new int[this.getStudents().size()];
		int i = 0;
		for (Object o:getStudents())
		{
			studentIDs[i] = ((Student)o).getId();
			i++;
		}
		int [] queryIDs = new int[this.getQueries().size()];
		int j = 0;
		for (Object o:getQueries())
		{
			queryIDs[j] = ((Query)o).getId();
			j++;
		}
		return new AcademicSupDescriptor(getId(),getFirstname(),getSurname(),getLogin(),getPassword(),getEmail(),studentIDs,queryIDs,getDepartment().getName(),getDepartment().getId());
	}
}
